// 获取商品列表
app.get('/goods', (req, res) => {
  let { pageNumber, pageSize, search } = req.query
  if (!pageSize) pageSize = 3
  if (!pageNumber) pageNumber = 1
  let newGoods = []
  if (search !== undefined && search !== '') {
    for (let i = 0; i < goods.length; i++) {
      if (goods[i].title.indexOf(search) >= 0) {
        newGoods.push(goods[i])
      }
    }
    //if (pageNumber > newGoods.length) {
    //pageNumber = newGoods.length;
    //}
    if (pageNumber > Math.ceil(newGoods.length / pageSize)) {
      pageNumber = Math.ceil(newGoods.length / pageSize)
    }
    if (pageNumber < 1) {
      pageNumber = 1
    }

    let arr = newGoods.slice(pageSize * (pageNumber - 1), pageSize * pageNumber)
    const result = {
      list: arr,
      totalCount: newGoods.length,
      currentPage: pageNumber,
    }
    res.send(result)
  } else {
    if (pageNumber > Math.ceil(goods.length / pageSize)) {
      pageNumber = Math.ceil(goods.length / pageSize)
    }
    newGoods = goods.slice(pageSize * (pageNumber - 1), pageSize * pageNumber)
    const result = {
      list: newGoods,
      totalCount: goods.length,
      currentPage: pageNumber,
    }
    res.send(result)
  }
})
